package by.grsu.verkhovodko.page;

import java.text.SimpleDateFormat;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;

import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.link.Link;


import by.grsu.verkhovodko.app.VerkhovodkoWebApplication;
import by.grsu.verkhovodko.dao.impl.BillDao;

import by.grsu.verkhovodko.entity.Bill;
import by.grsu.verkhovodko.entity.Passenger;
import by.grsu.verkhovodko.entity.Train;

public class TrainOrderPage extends WebPage {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2468399366671988587L;
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("DD.mm.yyyy HH:MM");
	private Long selectedPassengerId = new Long(0);
	
	
	@SuppressWarnings({ "rawtypes", "serial" })
	public TrainOrderPage(final Train train, final Passenger passenger) {
		super();
				

		final Form<Train> form = new Form<Train>("form");
		add(form);		
        
		form.add(new Label("passenger",passenger.getLogin()));
		form.add(new Label("id", train.getId()));
		form.add(new Label("destStation", train.getDestStation()));
		form.add(new Label("dateOfDeparture", sdf.format(train.getDateOfDeparture())));
		form.add(new Label("cost", train.getCost()));
        

		form.add(new SubmitLink("order") {
			@Override
		public void onSubmit() {
				super.onSubmit();
				
				System.out.println("let's order! train="+train+" passengerId="+selectedPassengerId);
			 	Bill bill = new Bill();					 	
				BillDao billDao = new BillDao(VerkhovodkoWebApplication.DATA_XML_FOLDER);
				
				bill.setTrainId(train.getId());
				bill.setPayerId(passenger.getId());
				System.out.println("save bill="+bill);
				billDao.saveNew(bill);				
				
				setResponsePage(new AfterOrder(passenger));
			}
			
		});
		
		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new TrainListPage());
			}
		});
	}
	
	
	
	
}
